$(document).ready(function(){
    // 当提交问答的表单中textarea获取焦点时触发事件
    var eTitleSec = $('#titleSec'), eTitle = $('#title'), eContentNav = $('#contentNav'), eContent = $('#content'), eSubmitSec = $('#submitSec'), eShadeSec = $('#shadeSec'), ePreviewBtn = $('#previewBtn');
    eContent.focus(function(){
        if(eContentNav.hasClass('hide')){
            eTitleSec.removeClass('hide');
            eSubmitSec.removeClass('hide');
            eContentNav.removeClass('hide');
            eContent.css('height',227);
            eTitle.focus();
        }
    });
    eTitle.focus(function(){
        if(uid == 0){
            eShadeSec.css({'height' : eContent.height() + 36, 'marginTop' : 0});
            eShadeSec.html('<span class="no-auth">如果您想发布问答，请您先 <a href="//wooo.wang/login.html">登录 · 注册</a> .</span>').addClass('md-preview-show');
            eContentNav.addClass('hide');
        }else{
            $(this).next('p').text('');
        }
    });
    // 模拟select功能
    var eSelectBtn = $('#selectBtn'), eSelectSec = $('#selectSec'), eSelectVal = $('#selectVal');
    eSelectBtn.on('click', function(){
        if(eSelectSec.hasClass('hide')) eSelectSec.removeClass('hide');
        else eSelectSec.addClass('hide');
    });
    eSelectSec.on('click', 'li', function(){
        eSelectBtn.text($(this).text());
        eSelectVal.val($(this).attr('val'));
        eSelectSec.addClass('hide');
    });
    // textarea框自动增高功能
    eContent.on('propertychange input', function(){
        var height = $(this)[0].scrollHeight;
        if(height >= 227){
            $(this).css('height', height);
        }
    });
    // 上传图片功能
    var key='';
    var uploader = Qiniu.uploader({
        runtimes: 'html5,html4',    //上传模式,依次退化
        browse_button: 'imgBtn',       //上传选择的点选按钮，**必需**
        uptoken : token,
        domain: 'http://img.wooo.wang/',
        //bucket 域名，下载资源时用到，**必需**
        //container: 'img',           //上传区域DOM ID，默认是browser_button的父元素，
        max_file_size: '4mb',           //最大文件体积限制
        flash_swf_url: '',  //引入flash,相对路径
        max_retries: 3,                   //上传失败最大重试次数
        dragdrop: false,                   //开启可拖曳上传
        drop_element: '',        //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
        chunk_size: '4mb',                //分块上传时，每片的体积
        auto_start: true,                 //选择文件后自动上传，若关闭需要自己绑定事件触发上传
        resize: {
            quality: 60,
            preserve_headers: false
        },
        init: {
            'FilesAdded': function(up, files) {
                plupload.each(files, function(file) {
                    eContent.val(eContent.val()+'\n![](http://img.wooo.wang/'+key+')\n').focus();
                });
            },
            'BeforeUpload': function(up, file) {
                // 每个文件上传前,处理相关的事情
            },
            'UploadProgress': function(up, file) {

            },
            'FileUploaded': function(up, file, info) {

            },
            'Error': function(up, err, errTip) {
                //上传出错时,处理相关的事情
            },
            'UploadComplete': function() {
                //队列文件处理完毕后,处理相关的事情
            },
            'Key': function(up, file) {
                // 该配置必须要在 unique_names: false , save_key: false 时才生效
                key = 'bbs/'+uid+'_'+oid+'_'+new Date().getTime()+'.png';
                return key;
            }
        }
    });
    // 显示隐藏输入markdown对应的文本
    ePreviewBtn.on('click', function(){
        if(eShadeSec.hasClass('md-preview-show')){
            eShadeSec.removeClass('md-preview-show');
        }else{
            eShadeSec.css('height', eContent.height());
            eShadeSec.html(marked(eContent.val())).addClass('md-preview-show');
        }
    });
    // 点击提交按钮时进行表单验证
    $('#submitBtn').click(function(e){
        e.preventDefault();
        if($(this).hasClass('no-save')){
            return;
        }
        if(uid != 0){
            if(eTitle.val() == ''){
                eTitle.next('p').text('请填写标题！');
            }else if(eContent.val() == ''){
                eContent.next('p').text('请书写一定的内容！');
            }else{
                $('#htmlContent').val(marked(eContent.val()));
                $('#form').submit();
            }
        }
    });
});